<template>
  <view class="home">
    <!-- 搜索栏 -->
    <my-search></my-search>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="">
      <swiper-item v-for="(item,index) in swiperData" :key="index">
        <navigator class="swiper-item" :url="'/subPackages/swiper_detail/swiper_detail?goods_id='+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类区域 -->
    <view class="nav-cate">
      <view class="nav-item" v-for="(item,index) in navData" :key="index" @click="switchTab(item)">
        <image :src="item.image_src" mode="widthFix"></image>
      </view>
    </view>
    <!-- 内容区域 -->
    <view class="floor">
      <view class="floor-item" v-for="(item,index) in floorData" :key="index">
        <view class="floor-item-title">
          <image :src="item.floor_title.image_src" mode="widthFix"></image>
        </view>
        <view class="floor-item-content">
          <view class="left">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix" @click="navigatorPage(item.product_list[0])">
            </image>
          </view>
          <view class="right">
            <view url="" v-for="(val,i) in item.product_list" :key="i" v-if="i !== 0" @click="navigatorPage(val)">
              <image :src="val.image_src" :style="{width:val.image_width+'rpx'}" mode="widthFix"></image>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import mixin from '@/mixin'
  var utils = require('../../common/tools.js')
  export default {
    mixins: [mixin],
    data() {
      return {
        // 轮播图数据
        swiperData: [],
        // 导航区域数据
        navData: [],
        // 楼层区域
        floorData: []
      }
    },
    // 生命周期
    onLoad() {
      this.getSwiperData()
      this.getNavData()
      this.getFloorData()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperData() {
        const {
          data
        } = await uni.$http.get('/home/swiperdata')
        data.meta.status === 200 ? (this.swiperData = data.message) : (this.swiperData = [])
      },
      // 获取导航区域数据
      async getNavData() {
        const {
          data
        } = await uni.$http.get('/home/catitems')
        data.meta.status === 200 ? (this.navData = data.message) : (this.navData = [])
      },
      // 点击分类跳转到分类页面
      switchTab(item) {
        item.open_type === 'switchTab' ? utils._switchTab('/pages/cate/cate') : utils._navigatorTo(
          '/subPackages/nav-detail/nav-detail', {
            name: item.name
          })
      },
      // 获取楼层数据
      async getFloorData() {
        const {
          data
        } = await uni.$http.get('/home/floordata')
        // 将请求参数的数据格式进行剥离
        data.message.forEach(item => {
          item.product_list.forEach(key => {
            let q = key.navigator_url.split('?')[1].split('=')
            key.query = {}
            key.query[q[0]] = q[1]
          })
        })
        data.meta.status === 200 ? (this.floorData = data.message) : (this.navData = [])
      },
      // 跳转到商品列表页面
      navigatorPage(item) {
        item.open_type === 'navigate' ? utils._navigatorTo('/subPackages/goods-list/goods-list', item.query) : ''
      }
    }
  }
</script>

<style lang="scss">
  .home {
    background-color: #fff;
  }

  swiper {
    height: 350rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-cate {
    display: flex;
    justify-content: space-around;
    padding: 20rpx 0;

    .nav-item {
      width: 25%;
      text-align: center;

      image {
        width: 70%;
      }
    }
  }

  .floor {
    .floor-item-content {
      display: flex;
      justify-content: space-around;
      margin: 0 10rpx;

      .right {
        display: flex;
        justify-content: space-around;
        flex-wrap: wrap;
      }
    }
  }
</style>
